package com.sneakxy.mybase4j.util;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.SocketAppender;
import org.apache.logging.log4j.core.net.AbstractSocketManager;
import org.apache.logging.log4j.core.net.Advertiser;

/**
 * @author 潜行的青衣
 * @see
 */
public class LogstashSocketAppender extends SocketAppender {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 485652307339068344L;

	private String[] extraKeys = new String[]{"mark"};
	
	private String[] extraValues;
	
	/**
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @param arg4
	 * @param arg5
	 * @param arg6
	 */
	protected LogstashSocketAppender(String arg0, Layout<? extends Serializable> arg1, Filter arg2,
			AbstractSocketManager arg3, boolean arg4, boolean arg5, Advertiser arg6) {
		super(arg0, arg1, arg2, arg3, arg4, arg5, arg6);
	}

	@Override
	public void append(LogEvent event) {
		if(extraKeys != null && extraValues != null && extraKeys.length == extraValues.length) {
			for(int i=0; i<extraKeys.length; i++) {
				event.getContextMap().put(extraKeys[i], extraValues[i]);
			}
		}
		super.append(event);
	}
	
	public void setExtraKeys(String extraKeys) {
		this.extraKeys = StringUtils.split(extraKeys, ",");
	}

	public void setExtraValues(String extraValues) {
		this.extraValues = StringUtils.split(extraValues, ",");
	}
	
}
